﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace XMLSpreadGear.Core
{
    internal static class Extensions
    {
        internal static byte ToByte(this bool? bolval)
        {
            if (bolval.HasValue && bolval.Value)
                return 1;
            else
                return 0;
        }

        internal static string MapPropertyType(this PropertyInfo propinfo)
        {
            DataType dtmap = DataType.String;

            switch (propinfo.PropertyType.Name)
            {
                case "String":
                    dtmap = DataType.String;
                    break;
                case "Int32":
                    dtmap = DataType.Number;
                    break;
                case "Int64":
                    dtmap = DataType.Number;
                    break;
                case "Int16":
                    dtmap = DataType.Number;
                    break;
                case "Double":
                    dtmap = DataType.Number;
                    break;
                case "Decimal":
                    dtmap = DataType.Number;
                    break;
                case "DateTime":
                    dtmap = DataType.DateTime;
                    break;
                case "bool":
                    dtmap = DataType.Boolean;
                    break;
                default:
                    dtmap = DataType.String;
                    break;
            }

            return Enum.GetName(typeof(DataType),dtmap);
        }

        internal static object GetValue(this PropertyInfo propinfo, object objitem)
        {
            object str = null;
            switch (propinfo.PropertyType.Name)
            {
                case "String":
                    str = propinfo.GetValue(objitem, null);
                    break;
                case "Int32":
                    str = propinfo.GetValue(objitem, null);
                    break;
                case "Double":
                    str = propinfo.GetValue(objitem, null);
                    break;
                case "DateTime":
                    str = ((propinfo.GetValue(objitem, null) == null) ? string.Empty : DateTime.Parse(propinfo.GetValue(objitem, null).ToString()).ToShortDateString());
                    break;
                case "bool":
                    str = propinfo.GetValue(objitem, null);
                    break;
                default:
                    str = propinfo.GetValue(objitem, null);
                    break;
            }
            return str;
        }
    }
}
